iT邦幫忙

2023 iThome 鐵人賽

DAY 28
0
Software Development

菜鳥工程師30天學java基礎系列 第 28

Day 28 Java Project : Income Statement

  • 分享至 

  • xImage
  •  

用java寫一個收益表


package com.example.smallchange;

import java.text.SimpleDateFormat;
import java.util.Scanner;
import java.util.Date;

public class SmallChangeSys {
    /*
     * Start with display board
     * 
     */
    public static void main(String[] args) {
        //define variable
        boolean loop = true;
        Scanner scanner = new Scanner(System.in);
        String key = "";
        String details="------------Detailed statement of accounts---------------";
        
        //income detail
        double money = 0;
        double balance = 0;
        Date date = null; //java.util.Date
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm");
        do{
            System.out.println("\n===========SmallChange===========");
            System.out.println("\t\t\t1. Detailed statement of accounts");
            System.out.println("\t\t\t2. Income");
            System.out.println("\t\t\t3. Cost");
            System.out.println("\t\t\t4. Exit");

            System.out.print("Please choose(1-4) :");

            key = scanner.next();
            //Use Switch case control
            switch(key){
                case "1":
                    System.out.println(details);
                    break;
                case "2":
                    System.out.print("Detail of Income 1");
                    money=scanner.nextDouble();
                    //money value must be positive number
                    
                    balance +=money;
                    //output detail of balance
                    date = new Date();//get date
                    details +="\nBalance\t"+money+"\t"+sdf.format(date)+"\t"+balance;
                    break;
                case "3":
                    System.out.println("3. Cost");
                    break;

                case "4":
                    System.out.println("4. Exit");
                    loop=false;
                    break;
                default:
                    System.out.println("Error,Choose again");
                    break;
            }

        }while (loop); 
        System.out.println("SmallChange closed");
    }
    

}

Result :
https://ithelp.ithome.com.tw/upload/images/20231013/20151606ntbu01aYBi.png
[寫到一半]


上一篇
Day 27 java 遞迴
下一篇
Day 29 Java 線程和進程
系列文
菜鳥工程師30天學java基礎30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言